package com.qixiaobao.nondirect.invoice.service.impl;

import com.qixiaobao.common.utils.StringUtils;
import com.qixiaobao.nondirect.invoice.domain.InvoiceStatistic;
import com.qixiaobao.nondirect.invoice.domain.InvoiceStatisticNew;
import com.qixiaobao.nondirect.invoice.domain.dto.NonDirectInvoiceStatisticDTO;
import com.qixiaobao.nondirect.invoice.mapper.InvoiceStatisticMapper;
import com.qixiaobao.nondirect.invoice.service.IInvoiceStatisticService;
import com.qixiaobao.system.domain.SignCompany;
import com.qixiaobao.system.mapper.SignCompanyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description 发票汇总 业务处理层
 * @Author yangxm
 * @Date 2025/7/8 11:08
 */
@Service
public class InvoiceStatisticServiceImpl implements IInvoiceStatisticService {

    @Autowired
    private InvoiceStatisticMapper invoiceStatisticMapper;

    /**
     * @Description 查询发票汇总列表
     * @Author yangxm
     * @Date 2025/7/8 11:08
     */
    @Override
    public List<InvoiceStatistic> selectInvoiceStatisticList(NonDirectInvoiceStatisticDTO nonDirectInvoiceStatisticDTO) {
        return invoiceStatisticMapper.selectInvoiceStatisticList(nonDirectInvoiceStatisticDTO);
    }

    /**
     * @Description 查询发票汇总（新）列表
     * @Author yangxm
     * @Date 2025/7/8 13:15
     * @param nonDirectInvoiceStatisticDTO
     * @return java.util.List<com.qixiaobao.nondirect.invoice.domain.InvoiceStatisticNew>
     */
    @Override
    public List<InvoiceStatisticNew> selectInvoiceStatisticNewList(NonDirectInvoiceStatisticDTO nonDirectInvoiceStatisticDTO) {
        return invoiceStatisticMapper.selectInvoiceStatisticNewList(nonDirectInvoiceStatisticDTO);
    }

    /**
     * @Description 发票汇总（新），各数据统计
     * @Author yangxm
     * @Date 2025/7/8 13:39
     * @param nonDirectInvoiceStatisticDTO
     * @return java.util.Map<java.lang.String,java.math.BigDecimal>
     */
    @Override
    public Map<String, BigDecimal> newTotal(NonDirectInvoiceStatisticDTO nonDirectInvoiceStatisticDTO) {
        Map<String, BigDecimal> map = new HashMap<>();
        List<InvoiceStatisticNew> list = selectInvoiceStatisticNewList(nonDirectInvoiceStatisticDTO);
        BigDecimal payableInvoiceAmountNewTotal = BigDecimal.ZERO;
        BigDecimal actualInvoiceAmountNewTotal = BigDecimal.ZERO;
        BigDecimal monthlyUnbilledAmountNewTotal = BigDecimal.ZERO;
        BigDecimal currentInvoicedAmountNewTotal = BigDecimal.ZERO;
        BigDecimal currentActualInvoiceAmountNewTotal = BigDecimal.ZERO;
        BigDecimal currentUnbilledAmountNewTotal = BigDecimal.ZERO;
        BigDecimal lastMonthUnbilledAmountNewTotal = BigDecimal.ZERO;
        BigDecimal monthlyOweAmountNewTotal = BigDecimal.ZERO;
        if (StringUtils.isNotEmpty(list)){
            payableInvoiceAmountNewTotal = list.stream().map(InvoiceStatisticNew::getPayableInvoiceAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
            actualInvoiceAmountNewTotal = list.stream().map(InvoiceStatisticNew::getActualInvoiceAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
            monthlyUnbilledAmountNewTotal = list.stream().map(InvoiceStatisticNew::getMonthlyUnbilledAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
            currentInvoicedAmountNewTotal = list.stream().map(InvoiceStatisticNew::getCurrentInvoicedAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
            currentActualInvoiceAmountNewTotal = list.stream().map(InvoiceStatisticNew::getCurrentActualInvoiceAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
            currentUnbilledAmountNewTotal = list.stream().map(InvoiceStatisticNew::getCurrentUnbilledAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
            lastMonthUnbilledAmountNewTotal = list.stream().map(InvoiceStatisticNew::getLastMonthUnbilledAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
            monthlyOweAmountNewTotal = list.stream().map(InvoiceStatisticNew::getMonthlyOweAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        }
        map.put("payableInvoiceAmountNewTotal", payableInvoiceAmountNewTotal);
        map.put("actualInvoiceAmountNewTotal", actualInvoiceAmountNewTotal);
        map.put("monthlyUnbilledAmountNewTotal", monthlyUnbilledAmountNewTotal);
        map.put("currentInvoicedAmountNewTotal", currentInvoicedAmountNewTotal);
        map.put("currentActualInvoiceAmountNewTotal", currentActualInvoiceAmountNewTotal);
        map.put("currentUnbilledAmountNewTotal", currentUnbilledAmountNewTotal);
        map.put("lastMonthUnbilledAmountNewTotal", lastMonthUnbilledAmountNewTotal);
        map.put("monthlyOweAmountNewTotal", monthlyOweAmountNewTotal);
        return map;
    }
}
